package com.spider.iceCream.spiderComponent;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class UrltoCid {
    public Integer urlToCid(String url) throws IOException {
        String result;Integer cid=0;
        Integer length = url.length();
        if (length <= 43) {
            String[] strings = url.split("video/");
            result = strings[1];
        } else {
            String[] strings = url.split("video/");
            String[] strings1 = strings[1].split("[?]");
            result = strings1[0];
        }
        url = "https://api.bilibili.com/x/player/pagelist?bvid=" + result + "&jsonp=jsonp";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet实例
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if (response != null) {
            HttpEntity entity = response.getEntity();
            String contain = EntityUtils.toString(entity, "UTF-8");
            String contain1 = String.valueOf(contain);
            String[] contain2 = contain1.split("cid\":");
            String contain3 = contain2[1];
            String[] contain4 = contain3.split(",\"page");
            String cid1 = contain4[0];
            cid = Integer.parseInt(cid1);
        }

        return cid;
    }
}
